package com.itheima.web.controllor.system;

import com.github.pagehelper.PageInfo;

import com.itheima.domain.system.Dept;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.impl.DeptServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet(urlPatterns="/system/dept")
public class DeptServlet extends HttpServlet {
    private DeptService deptService = new DeptServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if ("list".equals(operation)){
            //跳转部门列表
            this.list(req,resp);
        }else if ("toAdd".equals(operation)){
            //跳转到添加页面
            toAdd(req,resp);
        }else if ("save".equals(operation)){
            save(req,resp);
        }else if ("delete".equals(operation)){
            delete(req,resp);
        }else if ("toEdit".equals(operation)){
            toEdit(req,resp);
        }else if ("edit".equals(operation)){
            update(req,resp);
        }

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        Dept dept = BeanUtil.fillBean(req, Dept.class);
        int rst = deptService.update(dept);
        try {
            resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) {

        String id = req.getParameter("id");
        Dept dept = deptService.findById(id);
        req.setAttribute("dept",dept);

        try {
            req.getRequestDispatcher("/WEB-INF/pages/system/dept/update.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        int rst = deptService.delete(id);
        System.out.println(rst);

        try {
            resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
    添加操作
     */
    private void save(HttpServletRequest req, HttpServletResponse resp) {
        Dept dept = BeanUtil.fillBean(req, Dept.class);
        List<Dept> deptList = deptService.findAll();
        req.setAttribute("deptList",deptList);
        int rst = deptService.save(dept);
        System.out.println(rst);
        try {
            resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
    添加页面
     */
    private void toAdd(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("/WEB-INF/pages/system/dept/add.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    跳转页面
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) {
       //设置默认页面显示当前页面和条数
        int currentPage = 1;
        int pageSize = 3;
        //获取页面传递的当前页面和条数
        String page = req.getParameter("page");
        String size = req.getParameter("size");
        //获取用户想要显示页面形式，有值则赋值，没有默认
        if (StringUtils.isNotBlank(page)){
            currentPage = Integer.parseInt(page);
        }
        if (StringUtils.isNotBlank(size)){
            pageSize=Integer.parseInt(size);
        }
        //调用业务层
        PageInfo all = deptService.findAll(currentPage, pageSize);

        req.setAttribute("page",all);
        //跳转页面list
        try {
            System.out.println(req.getContextPath());
            req.getRequestDispatcher("/WEB-INF/pages/system/dept/list.jsp").forward(req,resp);
          //  resp.sendRedirect(req.getContextPath()+"/WEB-INF/pages/system/dept/list.jsp");
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
